package com.skyd.core.android.game.crosswisewar;

import java.util.ArrayList;

import android.graphics.Canvas;
import android.graphics.Rect;

import com.skyd.core.android.game.GameImageSpirit;
import com.skyd.core.android.game.GameMaster;
import com.skyd.core.android.game.GameScene;
import com.skyd.core.android.game.GameSpirit;
import com.skyd.core.android.game.GameObjectList.OnChangedListener;
import com.skyd.core.game.crosswisewar.EntityType;
import com.skyd.core.game.crosswisewar.IBuilding;
import com.skyd.core.game.crosswisewar.IEntity;
import com.skyd.core.game.crosswisewar.IIntangible;
import com.skyd.core.game.crosswisewar.INation;
import com.skyd.core.game.crosswisewar.IObj;
import com.skyd.core.game.crosswisewar.IScene;
import com.skyd.core.game.crosswisewar.IWarrior;
import com.skyd.core.vector.Vector2DF;
/**
 * 场景
 * @author SkyD
 *
 */
public class Scene extends GameScene implements IScene {
	
	public Scene(){
		this.getStandardDisplayAreaSize().reset(800, 480);
		this.getSpiritList().addOnChangedListener(new OnChangedListener() {

			@Override
			public void OnChangedEvent(Object sender, int newSize) {
				Scene.this.updateChildrenList();
			}
		});
	}

	@Override
	public float getHorizonPoint() {
		return 400;
	}

	@Override
	public float getLeftEnterPoint() {
		return -50;
	}

	@Override
	public float getRightEnterPoint() {
		return getTotalWidth() + 50;
	}

	@Override
	public int getTotalWidth() {
		return 2400;
	}

	/**
	 * 初始值为0。 功能注释：摄像机偏移量
	 */
	private float _CameraOffset = 0;

	/**
	 * 获取CameraOffset。 功能注释：摄像机偏移量
	 * 
	 * @return 当前的float类型的值
	 */
	public float getCameraOffset() {
//		float s = _CameraOffset;
//		float scale = this.getDisplayAreaFixScaleForSize().getX();
//		float screenwidth = GameMaster.getScreenWidth()/scale;
//		s=s-screenwidth/2;
//		if(s<screenwidth/2)s=screenwidth/2;
//		if(getTotalWidth()-s<screenwidth/2)s=getTotalWidth()-screenwidth/2;
//		return s;
		return _CameraOffset;
	}

	/**
	 * 设置CameraOffset。 功能注释：摄像机偏移量
	 * 
	 * @param value
	 *            要设置的float类型值
	 */
	public void setCameraOffset(float value) {
		float s = value;
		float scale = this.getDisplayAreaFixScaleForSize().getX();
		float screenwidth = GameMaster.getScreenWidth()/scale;
//		s=s-screenwidth/2;
		if(s<0)s=0;
		if(getTotalWidth()-s<screenwidth)s=getTotalWidth()-screenwidth;
		_CameraOffset=s;
//		AdjustmentBackground(_CameraOffset);
	}

//	/**
//	 * 调整背景图位置
//	 * @param offset 偏移量
//	 */
//	private void AdjustmentBackground(float offset) {
//		_BackgroundImage.getPositionOffset().reset(0-offset, 0);
//	}

	/**
	 * 初始值为null。 功能注释：背景图像。
	 */
	private GameImageSpirit _BackgroundImage = null;

	/**
	 * 设置背景图像精灵。应只在初始化时调用。
	 * @param value
	 */
	public void setBackgroundImage(GameImageSpirit value) {
		_BackgroundImage = value;
		this.getSpiritList().add(value);
		value.setLevel(-9999);
//		value.setIsStretchContent(true);
		value.getImage().getSize().reset(getTotalWidth(), 480);
//		value.setIsUseAbsoluteSize(true);
//		value.setIsUseAbsolutePosition(true);
//		value.getImage().setIsUseAbsolutePosition(true);
//		value.getImage().setIsUseAbsoluteSize(true);
//		AdjustmentBackground(getCameraOffset());
		value.addOnDrawingListener(new OnDrawingListener() {
			
			@Override
			public boolean OnDrawingEvent(Object sender, Canvas c, Rect drawArea) {
				float scale = Scene.this.getDisplayAreaFixScaleForSize().getX();
//				float screenwidth = GameMaster.getScreenWidth()/scale;
				
				c.translate(0-Scene.this.getCameraOffset()*scale, 0);
				c.save();
				return true;
			}
		});
	}

	@Override
	public ArrayList<IObj> getChildrenList() {
		return _ChildrenList;
	}

	ArrayList<IObj> _ChildrenList;
	
	/**
	 * 更新子对象列表
	 */
	void updateChildrenList(){
		_ChildrenList=null;
		_ChildrenList=new ArrayList<IObj>();
		for(GameSpirit f:getSpiritList()){
			if(f instanceof IObj)_ChildrenList.add((IObj) f);
		}
	}

	public GameImageSpirit getBackgroundImage() {
		return _BackgroundImage;
	}
	
	@Override
	public void moveCamera(Vector2DF v) {
		float scale = this.getDisplayAreaFixScaleForSize().getX();
		this.setCameraOffset(this.getCameraOffset()+v.getX()/scale);
	}

	@Override
	public float getPositionInScene(float xInScreen){
		float scale = this.getDisplayAreaFixScaleForSize().getX();
		return getCameraOffset()+xInScreen/scale;
	}
	
	@Override
	public IEntity[] findEntities(INation nation,EntityType type,float leftPos,float rightPos){
		ArrayList<IEntity> l=new ArrayList<IEntity>();
		for(IObj o:getChildrenList()){
			if(o instanceof IIntangible)continue;
			if((nation==null||o.getNation().equals(nation))&&(o.getPositionInScene().getX()>=leftPos&&o.getPositionInScene().getX()<=rightPos)&&((type==EntityType.WarriorAndBuilding&&o instanceof IEntity)||(type==EntityType.Building&&o instanceof IBuilding)||(type==EntityType.Warrior&&o instanceof IWarrior))){
				l.add((IEntity)o);
			}
		}
		return l.toArray(new IEntity[l.size()]);
	}
}
